<template>
  <div id="globalHeader">
    <a-row
      class="grid-demo"
      style="margin-bottom: 16px"
      align="center"
      :wrap="false"
    >
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          @menu-item-click="selectClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/logo.jpg" />
              <div class="title">海氦 OJ</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in showRoutes" :key="item.path"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div>{{ store.state.user?.loginUser?.userName }}</div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const store = useStore();
const router = useRouter();
const selectedKeys = ref(["/"]);

router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});
setTimeout(() => {
  store.dispatch("user/getLoginUser", {
    userName: "海氦管理员",
    userRole: ACCESS_ENUM.ADMIN,
  });
}, 3000);

const selectClick = (key: string) => {
  router.push({
    path: key,
  });
};

/*控制导航栏的显隐*/
const showRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideMenu) {
      return false;
    }
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #444;
  margin-left: 16px;

  .logo {
    padding: 0px 10px;
  }
}

.title {
  display: flex;
  align-items: center;
  justify-content: center;
}

.userName {
}
</style>
